﻿using Ncqrs;
using Ncqrs.Commanding.ServiceModel;
using Ncqrs.Eventing.Storage;
using System.Configuration;
using Ncqrs.Eventing.Storage.SQL;
using Ncqrs.Eventing.ServiceModel.Bus;
using Ncqrs.Commanding.CommandExecution.Mapping.Attributes;
using Domain.Commands;
using Domain.Events;
using ReadModel.ViewModels;


namespace Website.Code
{
    public class BootStrapper
    {
        public static void Boot()
        {
            NcqrsEnvironment.SetDefault(InitializeCommandService());
            NcqrsEnvironment.SetDefault(InitializeEventStore());
            NcqrsEnvironment.SetDefault(InitializeEventBus());
            NcqrsEnvironment.SetDefault(InitializeGuidGenerator());
        }


        private static IUniqueIdentifierGenerator InitializeGuidGenerator()
        {
            return new GuidCombGenerator();
        }

        public static ICommandService InitializeCommandService()
        {
            var service = new CommandService();
            service.RegisterExecutorsInAssembly(typeof(CreateNoteCommand).Assembly);

            return service;
        }

        public static IEventStore InitializeEventStore()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["EventStore"].ConnectionString;
            return new MsSqlServerEventStore(connectionString);
        }

        public static IEventBus InitializeEventBus()
        {
            var bus = new InProcessEventBus();
            bus.RegisterAllHandlersInAssembly(typeof(NoteListItem).Assembly);
            return bus;
        }
    }
}
